<?php
/**
 * Created by PhpStorm.
 * User: 张天硕
 * Date: 2017/3/21
 * Time: 11:47
 */

namespace app\admin\controller;

use \think\Controller;
class User extends Controller {

    // git冲突测试
    protected function filter(&$map)
    {
        if ($this->request->param('title')) {
            $map['title'] = ["like", "%" . $this->request->param('title') . "%"];
        }
        if ($this->request->param('name')) {
            $map['name'] = ["like", "%" . $this->request->param('name') . "%"];
        }
    }



    public function index(){
        $b = 111;

        $a = 1;
        echo time().'<br />';
        echo strtotime($a.' minute',time());die;
// /       $data = \think\Db::name('user')->select();
        $data = db('user')->select();
//        var_dump($data);die;
        $td = '';
        foreach($data as $key => $value){
            $td .= '<tr class="text-c"><td><input type="checkbox" name="id[]" value="2"></td>
                    <td>'.$value['userid'].'</td>
                    <td>'.$value['nickname'].'</td>
                    <td>'.$value['rename'].'</td>
                    <td>'.$value['email'].'</td>
                    <td><i class="Hui-iconfont c-success status" title="正常"></i></td>
                    <td>'.$value['password'].'</td>
                    <td class="f-14">
                        <a class="label radius ml-5 label-secondary" href="javascript:;"><i class="Hui-iconfont"></i>添加</a>
                        <a title="编辑" href="javascript:;" style="text-decoration:none" class="ml-5"><i class="Hui-iconfont"></i></a>
                        <a title="修改密码" href="javascript:;" class="label radius ml-5 label-secondary">修改密码</a>
                    </td></tr>';
        }
        return $this->fetch('index',['data'=>$td]);
    }

    //添加
    public function add(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $result = \think\Db::name('user')->insert($data);
            if($result){
                return $this->success('添加成功','user/index');
            }else{
                return $this->error('添加失败');
            }
        }
    }

    //编辑
    public function edit(){
        if($this->request->isPost()){
            $data = trim($this->request->post('param'));
//              echo $data;
            $userid = trim($this->request->post('userid'));
            $user =  db('user')->where(['userid'=>$userid])->find();
            $username = $user['username'];
            echo $username;
            die;

//              $result = \think\Db::name('user')->update($data);
//              if($result){
//                  echo 1;
////                  return $this->success('编辑成功','user/index');
//              }else{
//                  echo 2;
////                  return $this->error('编辑失败');
//              }
        }
    }

    //修改密码
    public function password(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $result = db('user')->where(['userid'=>$data['userid']])->update(['password'=>$data['password']]);
            if($result){
                return $this->success('密码修改成功','user/index');
            }else{
                return $this->error('密码修改失败');
            }
        }
    }

    //删除
    public function delete(){
        if($this->request->isPost()){
            $userid = $this->request->post('userid');
            var_dump($userid);die;
            $result = db('user')->where(['userid'=>$data['userid']])->delete();
            if($result){
                return $this->success('删除成功','user/index');
            }else{
                return $this->error('删除失败');
            }
        }

    }
}